import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
import _createClass from "@babel/runtime/helpers/esm/createClass";
import _inherits from "@babel/runtime/helpers/esm/inherits";
import _createSuper from "@babel/runtime/helpers/esm/createSuper";
import _applyDecoratedDescriptor from "@babel/runtime/helpers/esm/applyDecoratedDescriptor";
import _isEmpty from "lodash/isEmpty";

var _dec, _dec2, _dec3, _dec4, _dec5, _dec6, _dec7, _dec8, _dec9, _dec10, _dec11, _dec12, _dec13, _class, _class2;

import React, { PureComponent } from 'react';
import { Button, Drawer, Form, Input, Table, Row, Col } from 'antd';
import { Bind } from 'lodash-decorators';
import { Button as ButtonPermission } from 'components/Permission';
import intl from 'utils/intl';
import notification from 'utils/notification';
import { tableScrollWidth } from 'utils/utils';
import { SEARCH_FORM_ITEM_LAYOUT } from 'utils/constants';
import PermissionsModal from "./PermissionsModal";
import styles from "./index.less?modules";
var modelPrompt = 'ciam.menuConfig.model.menuConfig';
var commonPrompt = 'cpaas.common';
var Permissions = (_dec = Form.create({
  fieldNameProp: null
}), _dec2 = Bind(), _dec3 = Bind(), _dec4 = Bind(), _dec5 = Bind(), _dec6 = Bind(), _dec7 = Bind(), _dec8 = Bind(), _dec9 = Bind(), _dec10 = Bind(), _dec11 = Bind(), _dec12 = Bind(), _dec13 = Bind(), _dec(_class = (_class2 = /*#__PURE__*/function (_PureComponent) {
  _inherits(Permissions, _PureComponent);

  var _super = _createSuper(Permissions);

  function Permissions() {
    var _this;

    _classCallCheck(this, Permissions);

    for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }

    _this = _super.call.apply(_super, [this].concat(args));
    _this.state = {
      tableDataSource: [],
      tablePagination: {},
      tableSelectedRows: [],
      permissionsLovDataSource: [],
      permissionsLovPagination: {},
      permissionsLovVisible: false
    };
    _this.defautTableRowkey = 'id';
    return _this;
  }

  _createClass(Permissions, [{
    key: "componentDidMount",
    value: // getSnapshotBeforeUpdate(prevProps) {
    //   const { visible, permissionSetDataSource } = this.props;
    //   return (
    //     visible &&
    //     isInteger(permissionSetDataSource.key) &&
    //     permissionSetDataSource.key !== prevProps.permissionSetDataSource.key
    //   );
    // }
    // componentDidUpdate(prevProps, prevState, snapshot) {
    //   // If we have a snapshot value, we've just added new items.
    //   // Adjust scroll so these new items don't push the old ones out of view.
    //   // (snapshot here is the value returned from getSnapshotBeforeUpdate)
    //   if (snapshot) {
    //     this.fetchDataSource();
    //   }
    // }
    function componentDidMount() {
      this.fetchDataSource();
    } // 查询已分配的

  }, {
    key: "fetchDataSource",
    value: function fetchDataSource(params) {
      var _this2 = this;

      var _this$props = this.props,
          _this$props$handleQue = _this$props.handleQueryPermissionsBySet,
          handleQueryPermissionsBySet = _this$props$handleQue === void 0 ? function (e) {
        return e;
      } : _this$props$handleQue,
          _this$props$permissio = _this$props.permissionSetDataSource,
          permissionSetDataSource = _this$props$permissio === void 0 ? {} : _this$props$permissio;
      var fields = this.props.form.getFieldsValue();
      handleQueryPermissionsBySet(permissionSetDataSource.id, _objectSpread(_objectSpread({
        size: 10,
        page: 0
      }, fields), params)).then(function (res) {
        // const { dataSource, pagination } = res;
        _this2.setTableDataSource(res);
      });
    }
  }, {
    key: "setTableDataSource",
    value: function setTableDataSource(res) {
      this.setState({
        tableDataSource: res.dataSource || [],
        tablePagination: res.pagination || {},
        tableSelectedRows: [] // 重新查询后将选中数据清空

      });
    } // 查询可分配的

  }, {
    key: "fetchPermissionsLovDataSource",
    value: function fetchPermissionsLovDataSource(params) {
      var _this3 = this;

      var _this$props2 = this.props,
          _this$props2$handleQu = _this$props2.handleQueryPermissions,
          handleQueryPermissions = _this$props2$handleQu === void 0 ? function (e) {
        return e;
      } : _this$props2$handleQu,
          _this$props2$permissi = _this$props2.permissionSetDataSource,
          permissionSetDataSource = _this$props2$permissi === void 0 ? {} : _this$props2$permissi;
      handleQueryPermissions(permissionSetDataSource.id, _objectSpread(_objectSpread({
        size: 10,
        page: 0
      }, params), {}, {
        permissionSetId: permissionSetDataSource.id
      })).then(function (res) {
        var dataSource = res.dataSource,
            pagination = res.pagination;

        _this3.setState({
          permissionsLovDataSource: dataSource,
          permissionsLovPagination: pagination
        });
      });
    }
  }, {
    key: "onDrawerClose",
    value: function onDrawerClose() {
      var _this$props$close = this.props.close,
          close = _this$props$close === void 0 ? function (e) {
        return e;
      } : _this$props$close;
      this.setState({
        tableDataSource: [],
        tablePagination: {},
        tableSelectedRows: [],
        permissionsLovDataSource: [],
        permissionsLovPagination: {}
      });
      close();
    }
  }, {
    key: "onTableSelectedRowsChange",
    value: function onTableSelectedRowsChange(tableSelectedRowKeys, tableSelectedRows) {
      this.setState({
        tableSelectedRows: tableSelectedRows
      });
    }
  }, {
    key: "deletePermissions",
    value: function deletePermissions() {
      var _this4 = this;

      var tableSelectedRows = this.state.tableSelectedRows;
      var _this$props3 = this.props,
          onDeletePermissions = _this$props3.onDeletePermissions,
          permissionSetDataSource = _this$props3.permissionSetDataSource;
      var permissionCodes = [];
      tableSelectedRows.forEach(function (n) {
        permissionCodes.push(n.code);
      });
      var params = {
        menuId: permissionSetDataSource.id,
        permissionCodes: permissionCodes
      };

      if (onDeletePermissions) {
        onDeletePermissions(params).then(function (res) {
          if (res) {
            notification.success();

            _this4.fetchDataSource();
          }
        });
      }
    }
  }, {
    key: "openPermissionsLov",
    value: function openPermissionsLov() {
      this.fetchPermissionsLovDataSource();
      this.setState({
        permissionsLovVisible: true
      });
    }
  }, {
    key: "closePermissionsLov",
    value: function closePermissionsLov() {
      this.setState({
        permissionsLovVisible: false
      });
    }
  }, {
    key: "onPermissionsLovOk",
    value: function onPermissionsLovOk(selectedRows) {
      var _this5 = this;

      if (!(selectedRows.length > 0)) {
        notification.warning({
          message: intl.get('cpaas.common.validation.atLeast').d('请至少选择一条数据')
        });
        return;
      }

      var _this$props4 = this.props,
          onAssignPermissions = _this$props4.onAssignPermissions,
          permissionSetDataSource = _this$props4.permissionSetDataSource;
      var permissionCodes = [];
      selectedRows.forEach(function (n) {
        permissionCodes.push(n.code);
      });
      var params = {
        menuId: permissionSetDataSource.id,
        permissionType: 'PERMISSION',
        permissionCodes: permissionCodes
      };

      if (onAssignPermissions) {
        onAssignPermissions(params).then(function (res) {
          if (res) {
            notification.success();

            _this5.setState({
              permissionsLovVisible: false
            }, function () {
              _this5.fetchDataSource();
            });
          }
        });
      }
    }
  }, {
    key: "onTableChange",
    value: function onTableChange() {
      var pagination = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
      var _this$props$form$getF = this.props.form.getFieldsValue,
          getFieldsValue = _this$props$form$getF === void 0 ? function (e) {
        return e;
      } : _this$props$form$getF;
      var _pagination$current = pagination.current,
          current = _pagination$current === void 0 ? 1 : _pagination$current,
          _pagination$pageSize = pagination.pageSize,
          pageSize = _pagination$pageSize === void 0 ? 10 : _pagination$pageSize;
      this.fetchDataSource(_objectSpread({
        page: current - 1,
        size: pageSize
      }, getFieldsValue()));
    }
  }, {
    key: "handleFormReset",
    value: function handleFormReset() {
      var form = this.props.form;
      form.resetFields();
    }
  }, {
    key: "onCell",
    value: function onCell() {
      return {
        style: {
          overflow: 'hidden',
          maxWidth: 180,
          textOverflow: 'ellipsis',
          whiteSpace: 'nowrap'
        },
        onClick: function onClick(e) {
          var target = e.target;

          if (target.style.whiteSpace === 'normal') {
            target.style.whiteSpace = 'nowrap';
          } else {
            target.style.whiteSpace = 'normal';
          }
        }
      };
    }
  }, {
    key: "render",
    value: function render() {
      var _this6 = this;

      var _this$props5 = this.props,
          visible = _this$props5.visible,
          _this$props5$processi = _this$props5.processing,
          processing = _this$props5$processi === void 0 ? {} : _this$props5$processi,
          form = _this$props5.form,
          title = _this$props5.title,
          path = _this$props5.path;
      var _this$state = this.state,
          _this$state$tableData = _this$state.tableDataSource,
          tableDataSource = _this$state$tableData === void 0 ? [] : _this$state$tableData,
          _this$state$tablePagi = _this$state.tablePagination,
          tablePagination = _this$state$tablePagi === void 0 ? {} : _this$state$tablePagi,
          tableSelectedRows = _this$state.tableSelectedRows,
          permissionsLovVisible = _this$state.permissionsLovVisible,
          _this$state$permissio = _this$state.permissionsLovDataSource,
          permissionsLovDataSource = _this$state$permissio === void 0 ? [] : _this$state$permissio,
          _this$state$permissio2 = _this$state.permissionsLovPagination,
          permissionsLovPagination = _this$state$permissio2 === void 0 ? {} : _this$state$permissio2;
      var drawerProps = {
        title: title,
        visible: visible,
        mask: true,
        maskStyle: {
          backgroundColor: 'rgba(0,0,0,.85)'
        },
        placement: 'right',
        destroyOnClose: true,
        onClose: this.onDrawerClose,
        width: 800,
        wrapClassName: styles['ciam-menu-config-permissions']
      };
      var tableColumns = [{
        title: intl.get("".concat(modelPrompt, ".permissionCode")).d('权限编码'),
        dataIndex: 'code',
        onCell: this.onCell,
        width: 150
      }, {
        title: intl.get("".concat(modelPrompt, ".description")).d('描述'),
        dataIndex: 'description',
        onCell: this.onCell
      }, {
        title: intl.get("".concat(modelPrompt, ".path")).d('路径'),
        dataIndex: 'path',
        onCell: this.onCell,
        width: 150
      }, {
        title: intl.get("".concat(modelPrompt, ".method")).d('方法'),
        dataIndex: 'method',
        width: 80
      }, {
        title: intl.get("".concat(modelPrompt, ".level")).d('层级'),
        dataIndex: 'levelMeaning',
        width: 80
      }];
      var tableProps = {
        rowKey: this.defautTableRowkey,
        dataSource: tableDataSource,
        loading: processing.queryPermissionsByIdAll,
        columns: tableColumns,
        scroll: {
          x: tableScrollWidth(tableColumns)
        },
        pagination: tablePagination,
        onChange: this.onTableChange,
        bordered: true,
        rowSelection: {
          selectedRowKeys: tableSelectedRows.map(function (n) {
            return n[_this6.defautTableRowkey];
          }),
          onChange: this.onTableSelectedRowsChange
        }
      };
      var permissionsLovProps = {
        visible: permissionsLovVisible,
        dataSource: permissionsLovDataSource,
        pagination: permissionsLovPagination,
        selectedRows: tableDataSource,
        handleFetchDataSource: this.fetchPermissionsLovDataSource,
        onCancel: this.closePermissionsLov,
        onOk: this.onPermissionsLovOk,
        queryPermissionsByMenuId: processing.queryPermissionsByMenuId,
        assignPermissions: processing.assignPermissions
      };
      return /*#__PURE__*/React.createElement(Drawer, drawerProps, /*#__PURE__*/React.createElement(Form, {
        style: {
          marginBottom: 15
        }
      }, /*#__PURE__*/React.createElement(Row, {
        type: "flex",
        gutter: 24,
        align: "bottom"
      }, /*#__PURE__*/React.createElement(Col, {
        span: 8
      }, /*#__PURE__*/React.createElement(Form.Item, Object.assign({}, SEARCH_FORM_ITEM_LAYOUT, {
        label: intl.get("".concat(modelPrompt, ".catalogCode")).d('目录编码')
      }), form.getFieldDecorator('code')( /*#__PURE__*/React.createElement(Input, null)))), /*#__PURE__*/React.createElement(Col, {
        span: 8
      }, /*#__PURE__*/React.createElement(Form.Item, Object.assign({}, SEARCH_FORM_ITEM_LAYOUT, {
        label: intl.get("".concat(modelPrompt, ".conditionPermission")).d('描述/路径')
      }), form.getFieldDecorator('condition')( /*#__PURE__*/React.createElement(Input, null)))), /*#__PURE__*/React.createElement(Col, {
        span: 8
      }, /*#__PURE__*/React.createElement(Form.Item, null, /*#__PURE__*/React.createElement(Button, {
        onClick: this.handleFormReset,
        style: {
          marginRight: 8
        }
      }, intl.get("".concat(commonPrompt, ".button.reset")).d('重置')), /*#__PURE__*/React.createElement(Button, {
        type: "primary",
        htmlType: "submit",
        onClick: function onClick() {
          return _this6.fetchDataSource();
        },
        loading: processing.queryPermissionsById
      }, intl.get("".concat(commonPrompt, ".button.search")).d('查询')))))), /*#__PURE__*/React.createElement("div", {
        className: "action",
        style: {
          marginBottom: 15
        }
      }, /*#__PURE__*/React.createElement(ButtonPermission, {
        permissionList: [{
          code: "".concat(path, ".button.addPermission"),
          type: 'button',
          meaning: '菜单配置-添加权限'
        }],
        icon: "plus",
        style: {
          marginRight: 8
        },
        onClick: this.openPermissionsLov
      }, intl.get("".concat(modelPrompt, ".createPermission")).d('添加权限')), /*#__PURE__*/React.createElement(ButtonPermission, {
        permissionList: [{
          code: "".concat(path, ".button.deletePermission"),
          type: 'button',
          meaning: '菜单配置-删除权限'
        }],
        icon: "delete",
        onClick: this.deletePermissions,
        disabled: _isEmpty(tableSelectedRows),
        loading: processing.deletePermissions
      }, intl.get("".concat(commonPrompt, ".button.delete")).d('删除'))), /*#__PURE__*/React.createElement(Table, tableProps), /*#__PURE__*/React.createElement("br", null), /*#__PURE__*/React.createElement("div", {
        className: "footer"
      }, /*#__PURE__*/React.createElement(Button, {
        onClick: this.onDrawerClose,
        disabled: processing.save,
        type: "primary"
      }, intl.get("".concat(commonPrompt, ".button.close")).d('关闭'))), permissionsLovVisible && /*#__PURE__*/React.createElement(PermissionsModal, permissionsLovProps));
    }
  }]);

  return Permissions;
}(PureComponent), (_applyDecoratedDescriptor(_class2.prototype, "fetchDataSource", [_dec2], Object.getOwnPropertyDescriptor(_class2.prototype, "fetchDataSource"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "setTableDataSource", [_dec3], Object.getOwnPropertyDescriptor(_class2.prototype, "setTableDataSource"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "fetchPermissionsLovDataSource", [_dec4], Object.getOwnPropertyDescriptor(_class2.prototype, "fetchPermissionsLovDataSource"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "onDrawerClose", [_dec5], Object.getOwnPropertyDescriptor(_class2.prototype, "onDrawerClose"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "onTableSelectedRowsChange", [_dec6], Object.getOwnPropertyDescriptor(_class2.prototype, "onTableSelectedRowsChange"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "deletePermissions", [_dec7], Object.getOwnPropertyDescriptor(_class2.prototype, "deletePermissions"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "openPermissionsLov", [_dec8], Object.getOwnPropertyDescriptor(_class2.prototype, "openPermissionsLov"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "closePermissionsLov", [_dec9], Object.getOwnPropertyDescriptor(_class2.prototype, "closePermissionsLov"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "onPermissionsLovOk", [_dec10], Object.getOwnPropertyDescriptor(_class2.prototype, "onPermissionsLovOk"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "onTableChange", [_dec11], Object.getOwnPropertyDescriptor(_class2.prototype, "onTableChange"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "handleFormReset", [_dec12], Object.getOwnPropertyDescriptor(_class2.prototype, "handleFormReset"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "onCell", [_dec13], Object.getOwnPropertyDescriptor(_class2.prototype, "onCell"), _class2.prototype)), _class2)) || _class);
export { Permissions as default };